﻿using System;
using System.Linq;
using WebChat.Models;
using System.Collections.Generic;
using System.Net;
using System.Collections.Specialized;

namespace WebChat.Models
{
    /// <summary>
    /// Repository for meldinger
    /// </summary>
    public class MessageRepository
    {
        private ChannelDataClassesDataContext db; //Linq instansen
        private UserRepository userRep;
        /// <summary>
        /// Konstruktør
        /// </summary>
        public MessageRepository() 
        {
            db = new ChannelDataClassesDataContext();
            userRep = new UserRepository();
        }
        /// <summary>
        /// Metoden returnerer alle meldingene i DB
        /// </summary>
        /// <returns></returns>
        public IQueryable<Message> showAllMessages() 
        {
            return db.Messages;
        }

        /// <summary>
        /// Henter alle meldinger i chatroom. Bilder inkludert.
        /// </summary>
        /// <param name="id">Chatroom ID</param>
        /// <returns></returns>
        public IEnumerable<Message> showChatroomMessages(int id)
        {
            List<Message> messages = (from o in db.Messages
                                      where o.Chatroom == id
                                      orderby o.Date
                                      select o).ToList();

            //Knytter eventuelle bilder til tilhørende meldinger
            foreach (Message m in messages)
            {
                m.name = userRep.getUserName(m.AuthorId);
                if (m.imageId != null)
                {
                    m.imageType = getFile(m.imageId).imageType;
                    m.imageName = getFile(m.imageId).imageName;
                    m.imageSize = getFile(m.imageId).imageSize;
                }
            }
            return messages;
        }
        /// <summary>
        /// Henter ut en melding etter id
        /// </summary>
        /// <param name="id">Meldings id</param>
        /// <returns>message objekt</returns>
        public Message showMessage(int id)
        {
            Message message = (from o in db.Messages
                                where o.Id.Equals(id)
                                select o).FirstOrDefault();
            return message;
        }
        /// <summary>
        /// Metoden oppdaterer databasen etter hvile objekt som kommer inn.
        /// </summary>
        /// <param name="message">meldingsobjekt</param>
        public void updateMessage(Message message)
        {
            db.SubmitChanges();
        }
        /// <summary>
        /// Legger et nytt meldingsobjekt i databasen
        /// </summary>
        /// <param name="newMessage">Meldingsobjekt</param>
        public void addMessage(Message newMessage)
        {
            userRep.SendSMSToPhoneParticipants(newMessage);
            db.Messages.InsertOnSubmit(newMessage);
            db.SubmitChanges();
        }
        /// <summary>
        /// Sletter en melding fra databasen
        /// </summary>
        /// <param name="message">Meldingsobjekt</param>
        public void deleteMessage(Message message)
        {
            db.Messages.DeleteOnSubmit(message);
            db.SubmitChanges();
        }

        /// <summary>
        /// returnerer antall beskjeder i chatroom
        /// </summary>
        /// <param name="chatId">Chatroom ID</param>
        /// <returns></returns>
        public int getNoOfMessagesInroom(int chatId) 
        {
            List<Message> n = (from m in db.Messages
                               where m.Chatroom == chatId
                               select m).ToList();

            return n.Count;
                      
        }

        /// <summary>
        /// Blir gjort kall på etter GetCount i RoomApiController
        /// returnerer de nye meldingene som er kommet hvis det er kommet noen
        /// </summary>
        /// <param name="chatId">Chatroom ID</param>
        /// <param name="count">Antall</param>
        /// <returns></returns>
        public IEnumerable<Message> getNewMessages(int chatId, int count)
        {
            int i = 0;
            List<Message> m = (from o in db.Messages //Henter ut meldingene i chatroom
                               where o.Chatroom == chatId
                               select o).ToList();
            List<Message> newMess = new List<Message>();

            foreach (Message me in m)//Legger til de meldingne som er nye i newMess
            {
                i++;
                if (i > count)
                {
                    me.name = userRep.getUserName(me.AuthorId);
                    if (me.imageId != null)
                    {
                        me.imageType = getFile(me.imageId).imageType;
                        me.imageName = getFile(me.imageId).imageName;
                        me.imageSize = getFile(me.imageId).imageSize;
                    }
                    newMess.Add(me);
                }
            }

            return newMess;
        }

        /// <summary>
        /// Legger til fil i database som binærkode
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public int addImage(Image image)
        {
            db.Images.InsertOnSubmit(image);
            db.SubmitChanges();
            return image.id;
        }

        /// <summary>
        /// Henter fil fra database.
        /// </summary>
        /// <param name="id">Fil-ID</param>
        /// <returns></returns>
        public Image getFile(int? id)
        {
            Image fil = (from o in db.Images
                         where o.id.Equals(id)
                         select o).FirstOrDefault();
            return fil;
        }

        /// <summary>
        /// Sletter fil fra database
        /// </summary>
        /// <param name="fil"></param>
        public void deleteFile(Image fil)
        {
            db.Images.DeleteOnSubmit(fil);
            db.SubmitChanges();
        }

        /// <summary>
        /// Henter meldinger som inneholder inputstring
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public List<Message> showMatchMessages(string input)
        {
            List<Message> message = (from o in db.Messages
                                     where o.Message1.Contains(input)
                                     select o).ToList();
            return message;
        }
    }
}